#include "frisohelper.h"
#include "exception.h"

#include <QDebug>

FrisoHelper::FrisoHelper():
    friso(friso_new()), config(friso_new_config())
{
    int res = friso_init_from_ifile(friso, config, "friso.ini");
    if(res != 1)
    {
        qDebug() << "Load friso failed";
        throw AppException("Failed to load friso resources");
    }
    //自定义切分模式(简易，复杂，检测模式)
    //__FRISO_SIMPLE_MODE__
    //__FRISO_COMPLEX_MODE__
    //__FRISO_DETECT_MODE__
    friso_set_mode(config, __FRISO_COMPLEX_MODE__);
}

FrisoHelper::~FrisoHelper()
{
    friso_free(friso);
    friso_free_config(config);
}

std::vector<std::string> FrisoHelper::split(const std::string &source) const
{
    friso_task_t task = friso_new_task();
    fstring text = const_cast<char*>(source.c_str());
    friso_set_text(task, text);

    std::vector<std::string> result;
    while ((config->next_token(friso, config, task)) != NULL )
    {
        result.push_back(task->token->word);
    }

    friso_free_task(task);
    return result;
}

